package lambda05;

import org.junit.Test;

import java.util.Arrays;
import java.util.Random;
import java.util.function.Supplier;

/**
 *  Java中已经定义好的函数式接口
 *  Supplier接口: 适用于只获取结果,而不传递参数的操作
 *
 *  @FunctionalInterface
 *  public interface Supplier<T> {
 *     T get();
 *  }
 */
public class SupplierDemo {
    @Test
    public void test01(){
        //生成1~10之间的随机数
        int i = new Random().nextInt(10) + 1;
        System.out.println(i);
    }

    @Test
    public void test02(){
     /*   Supplier<Integer> s = () -> {
            return new Random().nextInt(10) + 1;
        };*/
        Supplier<Integer> s = () -> new Random().nextInt(10) + 1;//Supplier<Integer> s = new SupplierDemo$$Lambda01();
        System.out.println(s.get());
    }

    @Test
    public void test03(){
        //生成10个随机数,存储到数组中,最终将数组中的元素拼接成一个字符串返回
        int[] arr = new int[10];
        Supplier<Integer> s = () -> new Random().nextInt(10) + 1;

        for (int i = 0; i < 10; i++) {
            arr[i] = s.get();//获取一个随机数,就存储到数组的一个位置上
        }

        Supplier<String> s2 = ()->Arrays.toString(arr);//Supplier<String> s2 = new SupplierDemo$$Lambda02();
        System.out.println(s2.get());
    }
}

/*
  class SupplierDemo$$Lambda01 implements Supplier<Integer>{
      public Integer get(){
        return new Random().nextInt(10) + 1;
      }
  }
 class SupplierDemo$$Lambda02 implements Supplier<String>{
      public String get(){
        return Arrays.toString(arr);
      }
  }

 */
